package gzhaoku.com.machinemain;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.RemoteException;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.Toast;

import com.sunmi.printerhelper.utils.AidlUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;

import gzhaoku.com.common.BaseActivity;
import gzhaoku.com.common.jobs.CheckUpdateJob;
import gzhaoku.com.common.ui.annotations.Layout;
import gzhaoku.com.common.ui.annotations.Widget;
import gzhaoku.com.common.ui.components.XWebView;
import gzhaoku.com.common.util.AppUpdate;
import gzhaoku.com.common.util.CacheManager;
import gzhaoku.com.common.util.Certs;
import gzhaoku.com.common.util.StatusBarUtil;
import gzhaoku.com.machinemain.jssdk.SDK;
import gzhaoku.com.machinemain.jssdk.SDKListener;
import woyou.aidlservice.jiuiv5.IWoyouService;

@Layout(content = R.layout.activity_boot, fullscreen = false)
public class Boot extends BaseActivity {

    private static final int START_SCAN = 0x0001;

    @Widget(id=R.id.BootWebView)
    XWebView bootWebView;

    @Widget(id=R.id.loading)
    ImageView loadingView;

    SDK sdk;

    long backBtnClickTime = 0;

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        super.onActivityCreate(savedInstanceState);

        StatusBarUtil.setTransparent(this);

        watchUpdate();

        clearWebviewCache();

        sdk = new SDK(this, bootWebView, null);
        sdk.setListener(new TheSDKListener());
        bootWebView.addJavascriptInterface(sdk, "sdk");

        bootWebView.loadUrl(BuildConfig.MAIN_URL+"?v="+System.currentTimeMillis());

        //prepare printer
        AidlUtil.getInstance().connectPrinterService(Boot.this);

    }

    public void clearWebviewCache() {
        bootWebView.clearHistory();
    }

    private void watchUpdate(){
        new CheckUpdateJob(this).doJob();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode == KeyEvent.KEYCODE_BACK){
            if(bootWebView.canGoBack()){
                bootWebView.goBack();
                return true;
            }else{
                long now = System.currentTimeMillis();
                if(now - backBtnClickTime < 2000){
                    return super.onKeyDown(keyCode, event);
                }else{
                    Toast.makeText(this, "再按一次返回键退出", Toast.LENGTH_SHORT).show();
                    backBtnClickTime = now;
                    return true;
                }
            }

        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode ,int resultCode, Intent data){
        if(requestCode == START_SCAN && data != null){
            Bundle bundle = data.getExtras();
            ArrayList<HashMap<String, String>> result = (ArrayList<HashMap<String, String>>)bundle.getSerializable("data");
            Iterator<HashMap<String, String>> it = result.iterator();
            while(it.hasNext()){
                HashMap<String, String> map = it.next();
                Log.i("sunmi", map.get("TYPE"));
                Log.i("sunmi", map.get("VALUE"));
                //TODO 类型判断
                sdk.doScanQr(map.get("VALUE"));
            }
        }
    }

    void postDelayOnUiThread(final Runnable task, long time){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Boot.this.runOnUiThread(task);
            }
        }, time);
    }

    public class TheSDKListener extends SDKListener{

        @Override
        public void onPageReady(String url){

            postDelayOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if(loadingView != null){
                        loadingView.setVisibility(View.INVISIBLE);
                    }
                }
            }, 100);
        }
    }


}
